package cn.hj.domain.activity.service.quota;

import cn.hj.domain.activity.model.aggregate.CreateQuotaOrderAggregate;
import cn.hj.domain.activity.model.entity.*;
import cn.hj.domain.activity.model.vo.OrderTradeTypeVO;
import cn.hj.domain.activity.repository.IActivityRepository;
import cn.hj.domain.activity.service.IRaffleOrderAccountQuota;
import cn.hj.domain.activity.service.quota.chain.IActivityChain;
import cn.hj.domain.activity.service.quota.chain.factory.DefaultActivityChainFactory;
import cn.hj.domain.activity.service.quota.policy.ITradePolicy;
import cn.hj.types.enums.ResponseCode;
import cn.hj.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 抽象抽奖订单，定义抽奖订单流程
 */
@Slf4j
public abstract class AbstractQuotaRaffleActivityAccountQuota extends RaffleActivitySupportQuota implements IRaffleOrderAccountQuota {


    private final Map<String, ITradePolicy> tradePolicyMap;

    //为什么spring 推荐使用构造函数注入对象
    public AbstractQuotaRaffleActivityAccountQuota(IActivityRepository activityRepository, DefaultActivityChainFactory defaultActivityChainFactory, Map<String, ITradePolicy> tradePolicyMap) {

        super(activityRepository,defaultActivityChainFactory);
        this.tradePolicyMap = tradePolicyMap;
    }

    @Override
    public ActivityOrderEntity createRaffleActivityOrder(ActivityShopCatEntity activityShopCatEntity) {
        ActivitySkuEntity activitySkuEntity = activityRepository.queryActivitySku(activityShopCatEntity.getSku());
        ActivityEntity activityEntity = activityRepository.queryRaffleActivityByActivityId(activitySkuEntity.getActivityId());
        ActivityCountEntity activityCountEntity = activityRepository.queryRaffleActivityCountByActivityCountId(activitySkuEntity.getActivityCountId());
        log.info("查询结果{},{},{}", activitySkuEntity, activityEntity, activityCountEntity);
        return ActivityOrderEntity.builder().build();
    }

    @Override
    public String createRechangeOrder(SkuRechargeEntity skuRechargeEntity) {
        //参数校验
        String userId = skuRechargeEntity.getUserId();
        Long sku = skuRechargeEntity.getSku();
        String outBusinessNo = skuRechargeEntity.getOutBusinessNo();
        OrderTradeTypeVO orderTradeType = skuRechargeEntity.getOrderTradeType();
        if (userId == null || sku == null || outBusinessNo == null || orderTradeType == null) {
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(),ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }
        //查询基础信息
        ActivitySkuEntity activitySkuEntity = queryActivitySku(sku);
        ActivityEntity activityEntity = queryRaffleActivityByActivityId(activitySkuEntity.getActivityId());
        ActivityCountEntity activityCountEntity = queryRaffleActivityCountByActivityCountId(activitySkuEntity.getActivityCountId());
        //活动动作过滤
        IActivityChain activityChain = defaultActivityChainFactory.openActionChain();
        activityChain.action(activitySkuEntity, activityCountEntity, activityEntity);

        //构建订单聚合对象
        CreateQuotaOrderAggregate createOrderAggregate = buildOrderAggregate(skuRechargeEntity,activitySkuEntity,activityEntity,activityCountEntity);

        //保存订单
        ITradePolicy iTradePolicy = tradePolicyMap.get(skuRechargeEntity.getOrderTradeType().getCode());
        iTradePolicy.trade(createOrderAggregate);

        //返回单号
        return createOrderAggregate.getActivityOrderEntity().getOrderId();
    }

    @Override
    public UnpaidActivityOrderEntity createOrder(SkuRechargeEntity skuRechargeEntity) {
        //参数校验
        String userId = skuRechargeEntity.getUserId();
        Long sku = skuRechargeEntity.getSku();
        String outBusinessNo = skuRechargeEntity.getOutBusinessNo();
        OrderTradeTypeVO orderTradeType = skuRechargeEntity.getOrderTradeType();
        if (userId == null || sku == null || outBusinessNo == null || orderTradeType == null) {
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(),ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }

        //查询未支付订单「一个月以内的未支付订单」& 支付类型查询，非支付的走兑换
        if (OrderTradeTypeVO.credit_pay_trade.equals(skuRechargeEntity.getOrderTradeType())) {
            UnpaidActivityOrderEntity unpaidActivityOrder = activityRepository.queryUnpaidActivityOrder(skuRechargeEntity);
            if (unpaidActivityOrder != null) {
                return unpaidActivityOrder;
            }
        }

        //查询基础信息
        ActivitySkuEntity activitySkuEntity = queryActivitySku(sku);
        ActivityEntity activityEntity = queryRaffleActivityByActivityId(activitySkuEntity.getActivityId());
        ActivityCountEntity activityCountEntity = queryRaffleActivityCountByActivityCountId(activitySkuEntity.getActivityCountId());

        //活动动作过滤
        IActivityChain activityChain = defaultActivityChainFactory.openActionChain();
        activityChain.action(activitySkuEntity, activityCountEntity, activityEntity);

        //构建订单聚合对象
        CreateQuotaOrderAggregate createOrderAggregate = buildOrderAggregate(skuRechargeEntity,activitySkuEntity,activityEntity,activityCountEntity);

        //保存订单

        // 7. 交易策略 - 【积分兑换，支付类订单】【返利无支付交易订单，直接充值到账】【订单状态变更交易类型策略】
        ITradePolicy iTradePolicy = tradePolicyMap.get(skuRechargeEntity.getOrderTradeType().getCode());
        iTradePolicy.trade(createOrderAggregate);

        // 8. 返回订单信息
        ActivityOrderEntity activityOrderEntity = createOrderAggregate.getActivityOrderEntity();
        return UnpaidActivityOrderEntity.builder()
                .userId(userId)
                .orderId(activityOrderEntity.getOrderId())
                .outBusinessNo(activityOrderEntity.getOutBusinessNo())
                .payAmount(activityOrderEntity.getPayAmount())
                .build();
    }


    protected abstract CreateQuotaOrderAggregate buildOrderAggregate(SkuRechargeEntity skuRechargeEntity, ActivitySkuEntity activitySkuEntity, ActivityEntity activityEntity, ActivityCountEntity activityCountEntity);

}
